//index.js
//获取应用实例
var app = getApp()
import {
  checkInfoList,
  dealerOrder
} from '../../api/dealer'
Page({
  data: {
    orderNum: 0,
    sonNum: 0,
    list: [],
    list2:[],
    index: 1,
    isFinish: false,
    loadingMoreHidden: true,
    page: 1
  },
  takeGoodsTap(e) {
    let goodsId = e.currentTarget.dataset.id
    let orderid = e.currentTarget.dataset.orderid
    wx.removeStorageSync('stock-goods')
    wx.setStorageSync('stock-goods', e.currentTarget.dataset.item)
    console.log(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: "/pages/inferioraList/inferioraAddress"
    })
  },
  takeGoodsTap2(e) {
    let goodsId = e.currentTarget.dataset.id
    let orderid = e.currentTarget.dataset.orderid
    wx.removeStorageSync('stock-goods')
    wx.setStorageSync('stock-goods', e.currentTarget.dataset.item)
    console.log(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: "/pages/inferioraList/inferioraAddress2"
    })
  },
  toDetailsTap: function (e) {
    wx.navigateTo({
      url: `/pages/goods-detail/goods-detail?id=${e.currentTarget.dataset.id}`
    })
  },
  home: function () {
    wx.switchTab({
      url: "/pages/index/index"
    })
  },
  goToList() {
    wx.navigateTo({
      url: '/pages/inferioraList/inferioraList',
    })
  },
  onLoad() {
    wx.hideShareMenu()
  },
  async onShow() {
    var that = this;
    if (app.globalData.iphone == true) {
      that.setData({
        iphone: 'iphone'
      })
    }
    this.getData()
    if (app.globalData.uid) {
      app.get(`dealerorder/test_get_dealer_orders?DealerCustId=${app.globalData.uid}&Submitter=${app.globalData.uid}`, (res) => {
        if (res && res.code == "0") {
          console.log(res)
          that.setData({
            orderNum: res.count
          })
        }
      })
    }
    checkInfoList( app.globalData.dealerInfo.id, 1, wx.getStorageSync('access_token')).then(res => {
      console.log(res)
      this.setData({
        sonNum: res.data.children.total
      })
    })
  },
  async getData() {
    //获取商品列表
    let _this = this
    if (this.data.isFinish) {
      return
    }
    let res = await dealerOrder(this.data.page, wx.getStorageSync('access_token'))
    console.log(res)
    if (res && res.code == 10000) {
      if (res.data.records.length > 0) {
        _this.setData({
          list: _this.data.list.concat(res.data.records),
          page: this.data.page + 1
        })
        console.log(this.data.list)
      } else {
        _this.setData({
          isFinish: true,
        })
      }
    }
    // app.get(`dealer/get_dealer_by_custid?CustId=${app.globalData.uid}`,ret=>{
    //   if(ret&&ret.code=='0'){   
    //     let topId=ret.result.TopFromId        
    //     if(topId){
    //       app.get(`stock/get_sum_t_stock_record_groupby_goodslibid?CustId=${topId}`, res => {
    //         console.log(res)
    //         if (res && res.result.length > 0) { 
    //           this.setData({
    //             list2: res.result,
    //             isFinish: true
    //           })
    //         } else {
    //           this.setData({
    //             isFinish: false
    //           })
    //         }
    //       })
    //     }
    //   }
    // })
    // app.get(`stock/get_sum_t_stock_record_groupby_goodslibid?CustId=${app.globalData.uid}`, res => {
    //   if (res && res.result.length > 0) { 
    //     this.setData({
    //       list: res.result,
    //       isFinish: true
    //     })
    //   } else {
    //     this.setData({
    //       isFinish: false
    //     })
    //   }
    // })
  }
})